package com.cheer.manager;

import com.cheer.dao.StandingBookReviewDao;
import com.cheer.dao.StandingBookReviewLogDao;
import com.cheer.entity.StandingBookReview;
import com.cheer.entity.StandingBookReviewLog;
import com.cheer.enums.ReviewStatusEnum;
import com.cheer.enums.SubmitTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author cheer
 */
@Service
public class StandingBookReviewManager {

    @Autowired
    private StandingBookReviewDao standingBookReviewDao;

    @Autowired
    private StandingBookReviewLogDao standingBookReviewLogDao;

    /**
     * 提交审核
     *
     * @param accountId 账号Id
     * @param orgId     审核机构Id
     */
    public void submitReview(Long accountId, Long orgId) {
        submitReview(accountId, orgId, 0L);
    }

    /**
     * 提交到上级审核
     *
     * @param accountId   账号Id
     * @param reviewOrgId 上级审核机构Id
     * @param prevOrgId   当前审核机构Id
     */
    public void submitReview(Long accountId, Long reviewOrgId, Long prevOrgId) {
        StandingBookReview review = standingBookReviewDao.getByAccountIdAndOrgId(accountId, reviewOrgId);
        if (review == null) {
            review = new StandingBookReview();
            review.setAccountId(accountId);
            review.setReviewOrgId(reviewOrgId);
            review.setPrevOrgId(prevOrgId);
            review.setReviewStatus(ReviewStatusEnum.TO_REVIEW.getCode());
            standingBookReviewDao.insert(review);
        } else {
            StandingBookReview update = new StandingBookReview();
            update.setId(review.getId());
            if (review.getReviewStatus() == ReviewStatusEnum.NOT_PASSED.getCode()) {
                update.setReviewStatus(ReviewStatusEnum.NOT_PASSED_SUBMIT.getCode());
            } else if (review.getReviewStatus() == ReviewStatusEnum.REJECTED.getCode()) {
                update.setReviewStatus(ReviewStatusEnum.REJECTED_SUBMIT.getCode());
            } else {
                return;
            }
            update.setSubmitTime(new Date());
            standingBookReviewDao.updateById(update);
        }
    }

    /**
     * 更新审核状态
     *
     * @param review           审核流
     * @param reviewStatusEnum 审核状态
     * @param adminId          审核管理员Id
     */
    public void updateReview(StandingBookReview review, ReviewStatusEnum reviewStatusEnum, Long adminId) {
        // 更新审核状态
        StandingBookReview update = new StandingBookReview();
        update.setId(review.getId());
        update.setReviewStatus(reviewStatusEnum.getCode());
        standingBookReviewDao.updateById(update);
    }

}
